/**
 *	The NeuroCoSA Toolkit
 *	Copyright (C) 2003-6 Stuart Meikle.
 *
 *	This is free software; you can redistribute it and/or
 *	modify it under the terms of the GNU Lesser General Public
 *	License as published by the Free Software Foundation; either
 *	version 2.1 of the License, or (at your option) any later version.
 *
 *	This library is distributed in the hope that it will be useful,
 *	but WITHOUT ANY WARRANTY; without even the implied warranty of
 *	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *	Lesser General Public License for more details.
 *
 * @author	Stuart Meikle
 * @version	2006-halloween(mk2)
 * @license	LGPL
 */
package org.stumeikle.NeuroCoSA;
import java.io.*;

/**
 * Fundemental data packet structure, used for passing information between brain entities, and
 * between the brain and the body. Used as the base class for (external) Senses and Actions.
 * 
 */
public class DataBlock implements Serializable, Cloneable
{
    static final long 	serialVersionUID = DPUSerialID.DataBlock;
    /** Number used to make sure that lobes receive the correct information
     *  Important when senses and actions are relayed via TCP/IP connections.
     *  =sequence number.
     */

    private	long	iVersion;  /** data version, needed when sending packets via sockets to other systems
				    */
    private	long	iTimeStamp;/** used to ensure lobes receive senses which are newer
    				    *  than actions 
				    */

    public DataBlock()
    {
	iVersion 	=1;
	iTimeStamp	=-1;
    }

    synchronized public long	getVersion()
    {
	return iVersion;
    }
    
    		public	long	getTimeStamp()
    {
    	return iTimeStamp;
    }
    
    /** update the timestamp stored with the datablock. t is usually = Brain.getTime() 
     */
    		public	void	updateTimeStamp(long t) 
    {
        iTimeStamp= t;
    }

    synchronized public void	setVersion(long aL)
    {
	iVersion=aL;
    }

    /**
     * version number should be increased when the contents of the datablock
     * change.
     */
    synchronized public void	incrVersion()
    {
	setVersion( getVersion() + 1 );
    }

    /**
     * used to check if there is new data available since the previous version.
     * @return version of DataBlock if larger than aV else 0
     */
    public long changed(long aV)
    {
	//return true if version is different from v
	long gv = getVersion();

	if (gv > aV)
	    return gv;
	else
	    return 0;
    }

    /** Overload the clone method to change the access rights , to public
     */
    public Object clone()
    {
	try
	{	DataBlock c = (DataBlock) super.clone();
		
		return (Object) c;
	}
	catch (Exception e)
	{ System.out.println("DataBlock:can't clone datablock");
	  return null;
	}
    }
}
